package iso3.pt.action;

import iso3.pt.dao.IncorrectPasswordException;
import iso3.pt.dao.UserNotFoundException;
import iso3.pt.model.Alumno;
import iso3.pt.model.Asignatura;
import iso3.pt.model.Profesor;
import iso3.pt.service.PtDaoService;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

public class LoginAction extends ActionSupport implements Preparable {

	private static final long serialVersionUID = 1L;
	private String username;
	private String password;
	private String type;
	private Alumno student;
	private Profesor lecturer;
	private List<Asignatura> subjectList;
	private PtDaoService servicioHibernate;

	@SuppressWarnings("unchecked")
	public String doLogin() throws Exception {
		System.out.println("Validating login");
		servicioHibernate = new PtDaoService();
		if (getUsername().equals("") || getPassword().equals(""))
		{
			addActionError("Compulsory to specify both username and password!");
			return INPUT;
		}
		if (getType().equals("Profesor")) {
			try {
				Profesor profesor = servicioHibernate.loginProfesor(Integer.parseInt(getUsername()), getPassword());
				ActionContext.getContext().getSession().put("lecturer", profesor);
				return "listLecturerSubjects";

			} catch (IncorrectPasswordException ip) {
				addActionError("Incorrect password");
				return ERROR;
			} catch (NullPointerException ne) {
				return ERROR;
			} catch (UserNotFoundException ue) {
				return ERROR;
			}
		}
		else if (getType().equals("Estudiante")) {
			try {
				Alumno alumno = servicioHibernate.loginAlumno(Integer.parseInt(getUsername()), getPassword());
				ActionContext.getContext().getSession().put("student", alumno);
				return "listStudentSubjects";

			} catch (IncorrectPasswordException ip) {
				return ERROR;
			} catch (NullPointerException ne) {
				return ERROR;
			} catch (UserNotFoundException ue) {
				return ERROR;
			}

		}

		else {
			return SUCCESS;
		}
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String value) {
		password = value;
	}

	public List<Asignatura> getSubjects() {
		if (subjectList == null)
		{
			subjectList = new ArrayList<Asignatura>();
			Set<Asignatura> asigset = servicioHibernate.getAsignaturasProfesor(lecturer.getId());
			for (Iterator<Asignatura> iter = asigset.iterator(); iter.hasNext();)
			{
				Asignatura asig = iter.next();
				getSubjects().add(asig);

			}
		}
		return subjectList;
	}

	public String doLogout() {
		username = null;
		password = null;
		ActionContext.getContext().getSession().clear();
		return "logout";
	}

	@Override
	public void prepare() throws Exception {
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Alumno getStudent() {
		return student;
	}

	public void setStudent(Alumno student) {
		this.student = student;
	}

	public Profesor getLecturer() {
		return lecturer;
	}

	public void setLecturer(Profesor lecturer) {
		this.lecturer = lecturer;
	}
}